<template>
  <div>
    <Card style="margin-bottom: 10px">
      <Button type="primary" @click="AddData" icon="md-add">新增</Button>
    </Card>
    <Table
      ref="table"
      border
      :loading="loadingTable"
      :columns="tableTitle"
      :data="tableData"
    ></Table>
    <Modal
      :width="500"
      v-model="modalData"
      :title="tempData.Id > 0 ? '修改打印标签' : '新增打印标签'"
      :mask-closable="false"
    >
      <Form
        ref="formValidate"
        :model="tempData"
        :label-width="80"
        :rules="ruleValidate"
      >

        <FormItem label="标签名称" prop="ItemKey" >
          <Input  v-model="tempData.ItemKey" />
        </FormItem>
        <FormItem label="标签内容" prop="ItemValue">
          <Input   v-model="tempData.ItemValue" />
        </FormItem>

        <FormItem label="排序号" prop="SortNo">
          <Input type="number" v-model="tempData.SortNo" />
        </FormItem>
          <FormItem label="是否显示" prop="IsShow">
          <RadioGroup v-model="tempData.IsShow">
            <Radio :label="0">否</Radio>
            <Radio :label="1">是</Radio>
          </RadioGroup>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="modalData = false">取消</Button>
        <Button type="primary" :loading="loading" @click="SavePrintTag"
          >确定</Button
        >
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  GetPrintTag,
  DeletePrintTag,
  SavePrintTag
} from '@/api/data'
export default {
  name: 'printtag',
  data () {
    return {
      ruleValidate: {
        ItemKey: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ],
        ItemValue: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      loadingTable: false,
      modalData: false,
      tempData: {

      },
      tableTitle: [
        {
          type: 'index',
          title: '序号',
          align: 'center',
          width: 100
        },
        {
          title: '操作',
          align: 'center',
          width: 200,
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      var vm = this
                      vm.tempData = params.row
                      vm.modalData = true
                    }
                  }
                },
                '修改'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.DeletePrintTag(params.row.Id)
                    }
                  }
                },
                '删除'
              )
            ])
          }
        },
        {
          key: 'SortNo',
          title: '排序号',
          width: 100,
          align: 'center'
        },
        {
          key: 'IsShow',
          title: '是否显示',
          width: 100,
          align: 'center',
          render: (h, params) => {
            return h(
              'Tag',
              {
                props: {
                  color: params.row.IsShow == 0 ? 'orange' : 'green'
                }
              },
              params.row.IsShow == 0 ? '否' : '是'
            )
          }
        },
        {
          key: 'ItemKey',
          title: '标签名称',
          width: 150,
          align: 'center'
        },
        {
          key: 'ItemValue',
          title: '标签内容',
          minWidth: 200,
          align: 'center'
        }

      ],
      tableData: []
    }
  },
  computed: {},
  methods: {
    SavePrintTag () {
      let vm = this
      vm.$refs['formValidate'].validate((valid) => {
        if (valid) {
          vm.loading = true
          SavePrintTag(vm.tempData).then((res) => {
            vm.loading = false

            const data = res.data
            if (data.StatusCode == 200) {
              vm.modalData = false
              vm.$Message.success({ title: '提示', content: '保存成功' })
              vm.GetPrintTag()
            } else {
              vm.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        }
      })
    },
    GetPrintTag () {
      GetPrintTag(0).then((res) => {
        var data = res.data
        if (data.StatusCode == 200) {
          this.tableData = data.Data
        } else {
          vm.$Notice.error({
            title: data.Errors
          })
        }
      })
    },
    DeletePrintTag (id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要删除吗？',
        onOk: () => {
          DeletePrintTag(id).then((res) => {
            const data = res.data
            if (data.StatusCode == 200) {
              this.GetPrintTag()
              this.$Message.success({ title: '提示', content: '删除成功' })
            } else {
              this.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        },
        onCancel: () => {}
      })
    },
    AddData () {
      var vm = this
      vm.modalData = true
      vm.tempData = {
        IsShow: 1
      }
    }
  },

  mounted () {
    this.GetPrintTag()
  }
}
</script>

<style>
</style>
